package com.qianchang.microfinance.view.mine.bill;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineLinearLayout;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.CheckBankCardBean;
import com.qianchang.microfinance.bean.MyBillBean;
import com.qianchang.microfinance.bean.ThpinfoBean;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.tlpay.TlPayInputActivity;
import com.qianchang.microfinance.view.tlpay.TlUntiedBankCardActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/***
 * 修改成通联支付的支付方式的 2018.7.16
 */

public class TLPayActivity extends BaseActivity{

    @BindView(R.id.tv_forecast_id)
    DTextView tvForeCast;
    @BindView(R.id.oll_addCard_id)
    OneLineLinearLayout llAddCard;
    @BindView(R.id.oll_keepon_id)
    OneLineLinearLayout ollKeepon;
    @BindView(R.id.oll_untied_id)
    OneLineLinearLayout ollUntied;


    private MyBillBean.Bean bean;
    public static final String ENTITY_KEY = "MyBillBean_Bean";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tlpay_layout);
        ButterKnife.bind(this);
        initView();
        initData();
        isBindCard();

    }


    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_repayment));

    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        bean = (MyBillBean.Bean) bundle.getSerializable(MyBillAdapter.ENTITY_KEY);
        Log.i("wmy", "initData bean = " + bean.toString());

        tvForeCast.setText(bean.getPaymentAmount() + "");


    }

    private static int REQUECODE = 1;
    private static int RESCODE = 2;
    private static int UNTIED_REQUECODE = 3;
    private static int UNTIED_RESCODE = 4;
    private static int ADDCARD_REQUECODE = 5;
    private static int ADDCARD_RESCODE = 6;

    @OnClick({R.id.oll_keepon_id,R.id.oll_untied_id,R.id.oll_addCard_id})
    public void onViewClicked(View view) {
        int vId = view.getId();
        switch (vId){
            case R.id.oll_addCard_id:                //添加银行卡绑定
//                startActivity(TlPayInputActivity.class);            //跳转银行卡录入界面

                startActivityForResult(TlPayInputActivity.class,null,ADDCARD_REQUECODE);

                break;
            case R.id.oll_keepon_id:            //继续支付

                Bundle bundle = new Bundle();
                bundle.putSerializable(ENTITY_KEY, bean);                     // MyBillBean.Bean
                startActivityForResult(TLPayDetailsActivity.class,bundle,REQUECODE);        //跳转通联支付详情页面

                break;
            case R.id.oll_untied_id:            //解绑银行卡
                startActivityForResult(TlUntiedBankCardActivity.class,null,UNTIED_REQUECODE);        //跳转通联支付详情页面

                break;
            default:
                break;
        }
    }

    //判断是否绑定过银行卡显示不同视图
    private void setIsBindCardViewshow(boolean flag){
        if(flag){
            ollKeepon.setVisibility(View.VISIBLE);      //继续支付显示
            ollUntied.setVisibility(View.VISIBLE);      //解绑银行卡View 显示
            llAddCard.setVisibility(View.GONE);         //添加银行卡隐藏
        }else {
            ollKeepon.setVisibility(View.GONE);         //继续支付隐藏
            ollUntied.setVisibility(View.GONE);         //解绑银行卡View 隐藏
            llAddCard.setVisibility(View.VISIBLE);      //添加银行卡显示
        }
    }


    //是否绑定银行卡
    private void isBindCard(){

        String userId = DMApplication.getInstance().getmUserLoginInfo().getUserId();
        HttpParams params = new HttpParams();
        params.put("userId",userId);
        params.put("meruserid",userId);

        show();
        DMOKHttpUtils.getInstance().post(TLPayActivity.this, DMConstant.ApiURL.TL_CHECKBANKCARD, params, new RequestCallBack<CheckBankCardBean>() {

            @Override
            public void onSuccess(JsonResult<CheckBankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    CheckBankCardBean checkBankCardBean = response.getObject();
                    if(checkBankCardBean != null && checkBankCardBean.getTonglianAggreId() != null && (!checkBankCardBean.getTonglianAggreId().equals(""))){            //TonglianAggreId 如果不为空时绑定过银行卡，否则没有绑过
                        setIsBindCardViewshow(true);
                    }else {
                        setIsBindCardViewshow(false);
                    }
                }else {
                    setIsBindCardViewshow(false);
//                    ToastUtil.getInstant().show(TLPayActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(TLPayActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }




    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUECODE && resultCode == RESCODE){                                  //支付成功返回结束当前页面
            TLPayActivity.this.finish();
        }else if(requestCode == UNTIED_REQUECODE && resultCode == UNTIED_RESCODE){              // 从 TlUntiedBankCardActivity 解绑银行卡成功回调刷新视图
//            setIsBindCardViewshow(false);
            isBindCard();
        }else if(requestCode == ADDCARD_REQUECODE && resultCode == ADDCARD_RESCODE){            //绑卡成功从 TlPayInputActivity 页面返回刷新当前视图
            isBindCard();
        }


    }



}
